'use strict';

module.exports = app => {
    return class BannerService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        /**
         * 社区首页数据
         * @param params
         */
        async forumIndex(params) {
            let url = this.app.urls('forumIndex');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: params
            });
			// this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 查询所有分类
         * @param params
         * @returns {Promise<void>}
         */
        async getAllType(params) {
            let url = this.app.urls('forumType');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: params
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 根据分类id查询改分类下的所有type
         * @param forumTypeId
         * @returns {Promise<void>}
         */
        async getTypeChildren({ forumTypeId }) {
            let url = this.app.urls('forum');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    forumTypeId
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 社区详情
         * @param id
         * @returns {Promise<void>}
         */
        async detail({ id }) {
            let url = this.app.urls('forumDetail');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 查询
         * @param keyword
         * @returns {Promise<void>}
         */
        async search({ keyword }) {
            let url = this.app.urls('search');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    searchName: keyword
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }





    }

};
